﻿// ***********************************************************************
// Assembly         : Zf3DPlatform
// Author           : tianteng
// Created          : 01-23-2018
//
// Last Modified By : tianteng
// Last Modified On : 01-23-2018
// ***********************************************************************
// <copyright file="TianTengTestCmd.cs" company="北京智帆高科科技邮箱公司">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************

using System.Collections.Generic;
using System.IO;
using OsgEarth;
using OsgEarth.Symbology;
using Osg;
using ZFGK.Actions;
using ZFGK.Addins;
using ZFGK.IO;
using ZFGK.OSG.Utility;
using ZFGK.osgEarth.UI;
using ZFGK.osgEarth.UI.Actions;
using ZFGK.osgEarth.UI.Utility;
using ZFGK.osgEarth.Utility;
using ZFGK.Utility;
using Style = OsgEarth.Symbology.Style;

namespace OSS.Earth.Commands
{
    /// <summary>
    /// Class TianTengTestCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class TianTengTestCmd : IExtendCommand
    {
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var form = cmdData.ViewForm as OsgEarthViewForm;
            var osgEarthViewCtrl = form.OsgEarthViewCtrl;
            var actionUtility = osgEarthViewCtrl.ActionUtility;

            Vec3d mapPt1;
            if (!actionUtility.PickEarthPoint("选择第一个点", out mapPt1))
                return CmdResult.Cancel;

            var srs = osgEarthViewCtrl.OsgEarthObj.MapNode.getMapSRS();
            var mat = mapPt1.CreateLocalToWorld(srs);

            Vec3d mapPt2;
            if (!actionUtility.PickEarthPoint("选择第er个点", out mapPt2, delegate(PickEarthPointAction action)
            {
                action.OnMouseMove += delegate(BaseAction.BaseActionMouseEventArgs args)
                {
                    var x = args.MouseArgs.X;
                    var y = osgEarthViewCtrl.Size.Height - args.MouseArgs.Y;
                    Vec3d mapPt;
                    if (osgEarthViewCtrl.OsgEarthObj.GetMapPtUnderMouse(x, y, out mapPt))
                    {
                        var ucspt = mapPt.ToWorld(srs) * mat.GetInvertion();
                        if (ucspt.length < 0.1)
                            return;

                        var list = new List<Vec3f>();
                        list.Add(new Vec3f(0, -0.5f, 0));
                        list.Add(new Vec3f((float)ucspt.length, -0.5f, 0));
                        list.Add(new Vec3f((float)ucspt.length, 0.5f, 0));
                        list.Add(new Vec3f(0, 0.5f, 0));
                        var geode = OsgGeometryUtil.CreatePillar(list, 10, new Vec4f(1, 1, 1, 1)).CreateGeode();

                        var rotate = Matrixd.rotate((float)ucspt.ToVec2d().Angle(), new Vec3f(0, 0, 1));

                        var matrixTransform = new MatrixTransform(rotate * mat);
                        matrixTransform.addChild(geode);
                        matrixTransform.Name = "PickEarthNode";

                        osgEarthViewCtrl.OsgEarthObj.GUIEventHandler.DoOnce(delegate()
                        {
                            osgEarthViewCtrl.OsgObj.OtherModelGroup.AddOrReplaceNode(matrixTransform);
                            return false;
                        });

                    }
                };
            }))
                return CmdResult.Cancel;


            return CmdResult.Succeed;
        }

        private static void NewMethod()
        {
            var ellipsoidModel = new EllipsoidModel();

            Matrixd mat;
            ellipsoidModel.computeLocalToWorldTransformFromLatLongHeight(30.0.AngleToRadian(), 120.0.AngleToRadian(), 0,
                out mat);
            var pt = mat * new Vec3d(100, 100, 100);
            //MessageBox.Show(pt.ToJoinString());

            double X, Y, Z;
            ellipsoidModel.convertLatLongHeightToXYZ(30.0.AngleToRadian(), 120.0.AngleToRadian(), 0, out X, out Y, out Z);
        }

        private static CmdResult InsertNode(OeViewActionUtility actionUtility, SpatialReference srs)
        {
            Vec3d mapPt = null;
            // 选择插入点
            if (!actionUtility.PickEarthPoint("放置模型", out mapPt))
            {
                return CmdResult.Cancel;
            }

            var globeNode = new GeoTransform();
            globeNode.setPosition(new GeoPoint(srs, mapPt));
            //globeNode.addChild(osgDB.osgDB.readNodeFile(@"D:\OSG3.4.0+osgEarth2.8.0+vc10_x64\osg3.4.0\data\cow.osgt"));
            globeNode.addChild(
                OsgDB._.readNodeFile(
                    @"D:\工作资料\智帆高科\配电网三维平台\Source\DistributionNetwork3dPlatform\Build\Data\model\Pole\ZA-1.ive"));

            // 放置模型
            //             var osgNode = new OsgNode("TEST");
            //             osgNode.AddNode(new NodeData("1", globeNode));
            //             var form = cmdData.ViewForm as OsgEarthViewForm;
            // 
            //             form.OsgEarthViewCtrl.AddOrReplaceModel("TEST", globeNode, "1");
            return CmdResult.Succeed;
        }

        private CmdResult MyMethod()
        {
            string path = @"E:\Users\tianteng\Desktop\OSG";
            string[] fileNames = DirectoryUtil.GetFileNames(path, "*.osg", false);

            Group groupX = new Group();
            foreach (var fileName in fileNames)
            {
                groupX.addChild(OsgDB._.readNodeFile(fileName));
            }
            OsgDB._.writeNodeFile(groupX, Path.Combine(path, "result.ive"));
            OsgDB._.writeNodeFile(groupX, Path.Combine(path, "result.3ds"));
            OsgDB._.writeNodeFile(groupX, Path.Combine(path, "result.obj"));

            string path1 = Path.Combine(path, "0314A01B166A001.osg");
            string path2 = Path.Combine(path, "0314A01B166A001.osgb");
            OsgDB._.writeNodeFile(OsgDB._.readNodeFile(path1), path2);
            return CmdResult.Succeed;
        }

    }
}
